package co2.engine;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import org.hibernate.Session;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import co2.pojos.BankMotivasi;
import co2.pojos.BankSoal;
import co2.pojos.ChartPojo;
import co2.pojos.FileUpload;
import co2.pojos.HasilMotivasi;
import co2.pojos.HasilTest;
import co2.pojos.Kelompok;
import co2.pojos.Material;
import co2.pojos.MindMenu;
import co2.pojos.MstRole;
import co2.pojos.MstUser;
import co2.pojos.PR;
import co2.pojos.PaketMotivasi;
import co2.pojos.PaketSoal;
import co2.pojos.Silabus;
import co2.pojos.SoalLks;
import co2.pojos.ViewUser;

public class MasterDAOImp extends HibernateDaoSupport implements DAOInterface{


	public Session getCurrentSession(){
		Session session = null;
	     try
	     {
	       session = getHibernateTemplate().getSessionFactory().getCurrentSession();
	     } catch (Exception e) {
	    	 e.printStackTrace();
	     }
	     return session;
	  }
	
	public void save(Object obj) throws DataIntegrityViolationException, Exception{
		getHibernateTemplate().save(obj);
	}
	
	public void update(Object obj) throws Exception{
		getHibernateTemplate().update(obj);
	}
	
	public void delete(Class clazz, Serializable id) throws Exception{
		  
			Object object = get(clazz, id);
			getHibernateTemplate().delete(object);
		 
	}
	 public Object get(Class clazz, Serializable id){
		 return getHibernateTemplate().get(clazz, id);
	}

	@Transactional
	public List<MindMenu> getMindMenuByParent(String parent) {
		List<MindMenu> list = null;
		try{
			list = getCurrentSession().getNamedQuery("MindMenu#getByParent").setString("parent", parent).list();
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}

	@Transactional
	public List<MstUser> getAllMstUser() {
		List<MstUser> list = getCurrentSession().getNamedQuery("MstUser#getAll").list();
		
		return list;
	}

	@Transactional
	public List<MstRole> getAllRole() {
		List<MstRole> list = getCurrentSession().getNamedQuery("MstRole#getAll").list();
		return list;
	}

	@Transactional
	public List<Material> getAllMaterial() {
		List<Material> list = getCurrentSession().getNamedQuery("Material#getAll").list();
		return list;
	}

	@Transactional
	public List<Material> getAllMaterialByRole(String role) {
		
		List<Material> list = getCurrentSession().getNamedQuery("Material#getAllByRole").setString("role", role).list();
		
		return list;
	}

	@Transactional
	public List<ChartPojo> getAllChartPojo() {
		List<ChartPojo> list = getCurrentSession().getNamedQuery("Chartpojo#selectAll").list();
		return list;
	}

	@Transactional
	public List<PaketSoal> getAllPaketSoal() {
		
		return getCurrentSession().getNamedQuery("PaketSoal#getAll").list();
	}

	private void shuffleList(List list){
		long seed = System.nanoTime();
		Collections.shuffle(list, new Random(seed));
	}
	
	@Transactional
	public List<BankSoal> getBankSoalByPsId(String psId) {
		List<BankSoal> list = getCurrentSession().getNamedQuery("BankSoal#getAll").setString("id", psId).list();
		
		shuffleList(list);
		
		PaketSoal ps = null;
		int no = 1;
		if(list!=null && list.size()>0){
			for(BankSoal bs:list){
				ps = bs.getPaketSoal();
				ps.getId();ps.getPaketTitle();ps.getDescription();
				bs.setNo(""+no);
				if(bs.getSoal().length()>20){
					bs.setSumary(bs.getSoal().substring(0,20)+"...");
				}else{
					bs.setSumary(bs.getSoal());
				}
				no++;
			}
		}
		return list;
	}

	@Transactional
	public List<MstUser> getMstUserByAssignPaket(String paketId) {
		List<MstUser> list = null;
		if(paketId == null){
			list = getCurrentSession().getNamedQuery("MstUser#getByPaketIsnull").list();
		}else{
			list = getCurrentSession().getNamedQuery("MstUser#getByPaketid").setString("paketId", paketId).list();
		}
		
		if(list!=null && list.size()>0){
			for(MstUser m:list){
				if(m.getImage() == null){
					m.setImage("/images/dock/user_male_portrait.png");
				}
			}
		}
		
		return list;
		
	}

	@Override
	public List<ViewUser> getUserForAssignmentView() {
		List<ViewUser> list = getCurrentSession().getNamedQuery("viewUser#getAll").list();
		
		return list;
	}

	@Override
	public List<HasilTest> hasiltestExist(String userId, String paketId) {
		List list = getCurrentSession().getNamedQuery("hasilTest#getForValidation")
				.setString("userId", userId).setString("paketId", paketId)
				.list();
		
		return list;
	}

	@Override
	public List<HasilTest> getAllHasilTest() {
		List<HasilTest> list = getCurrentSession().getNamedQuery("hasilTest#getAll").list();
		
		bypassProxyHasilTest(list);
		
		return list;
	}
	
	public void bypassProxyHasilTest(List<HasilTest> list){
		if(list!=null && list.size()>0){
			for(HasilTest ht:list){
				ht.getId();
				MstUser mu = ht.getMstUser();
				if(mu!=null){
					mu.getUserId();mu.getUsername();
				}
				
				PaketSoal ps = ht.getPaketSoal();
				if(ps!=null){
					ps.getId(); ps.getPaketTitle();
				}
			}
		}
	}
	@Override
	public List<HasilTest> getAllByPaketSoalId(String paketId) {
		List<HasilTest> list = getCurrentSession().getNamedQuery("hasilTest#getAllByPaketSoalId").setString("paketId", paketId).list();
		
		bypassProxyHasilTest(list);
		
		return list;
	}

	@Override
	public List<Silabus> getAllSilabus() {
		List<Silabus> list = getCurrentSession().getNamedQuery("Silabus#getAll").list();
		
		return list;
	}

	@Override
	public List<PaketMotivasi> getAllPaketMotivasi() {
		List<PaketMotivasi> list = getCurrentSession().getNamedQuery("PaketMotivasi#getAll").list();
		return list;
	}

	@Override
	public List<BankMotivasi> getSoalByPaketMotivasi(String psId) {
		List<BankMotivasi> list = getCurrentSession().getNamedQuery("BankMotivasi#getAll").setString("psId", psId).list();
			if(list!=null&&list.size()>0){
				int no=1;
				for(BankMotivasi bm:list){
					PaketMotivasi pm = bm.getPaketMotivasi();
					if(pm!=null){
						pm.getId();pm.getPaketTitle();
					}
					
					bm.setNo(""+no);
					if(bm.getSoal().length()>20){
						bm.setSumary(bm.getSoal().substring(0,20)+"...");
					}else{
						bm.setSumary(bm.getSoal());
					}
					no++;
				}
			}
		return list;
	}

	@Override
	public List<MstUser> getMstUserByAssignMotivasi(String paketId) {
		List<MstUser> list = null;
		if(paketId == null){
			list = getCurrentSession().getNamedQuery("MstUser#getByPaketMotivasiIsnull").list();
		}else{
			list = getCurrentSession().getNamedQuery("MstUser#getByPaketMotivasiId").setString("paketId", paketId).list();
		}
		
		if(list!=null && list.size()>0){
			for(MstUser m:list){
				if(m.getImage() == null){
					m.setImage("/images/dock/user_male_portrait.png");
				}
			}
		}
		
		return list;
		
	}

	@Override
	public List<ViewUser> getAllForAssignmentViewMotivasi() {
		List<ViewUser> list = getCurrentSession().getNamedQuery("viewUser#getAllForPaketMotivasi").list();
		
		return list;
	}

	@Override
	public List<HasilMotivasi> getAllHasilMotivasiByUserId(String userId) {
		List<HasilMotivasi> list = getCurrentSession().getNamedQuery("haslMotivasi#getByUserId").setString("userId", userId)
				.list();
		bypassProxyHasilMotivasi(list);
		return list;
	}
	
	public void bypassProxyHasilMotivasi(List<HasilMotivasi> list){
		if(list!=null&&list.size()>0){
			for(HasilMotivasi hm:list){
				MstUser mu = hm.getMstUser();
				mu.getUserId();mu.getUsername();
				
				BankMotivasi bm = hm.getBankMotivasi();
				bm.getId();bm.getSoal();
				if(bm.getSoal()!=null&&bm.getSoal().length()>20){
					bm.setSumary(bm.getSoal().substring(0,21));
				}else{
					bm.setSumary(bm.getSoal());
				}
			}
		}
	}

	@Override
	public List<HasilMotivasi> getAllHasilMotivasiByUserIdAndPaket(
			String userId, String psId) {
		List<HasilMotivasi> list = getCurrentSession().getNamedQuery("haslMotivasi#getByUserIdAndPaket")
				.setString("userId", userId).setString("psId", psId).list();
		bypassProxyHasilMotivasi(list);
		return list;
	}

	@Override
	public Kelompok getKelompokForUser(String userId) {
		List<Kelompok> list = getCurrentSession().getNamedQuery("kelompok#ambilKelompoknyaSiUser").setString("userId", userId)
				.list();
		Kelompok kel = null;
		
		if(list!=null&&list.size()>0){
			kel = list.get(0);
		}
		return kel;
	}

	@Override
	public List<MstUser> getDaftarAnggotaKelompok(String kelompokId) {
		List<MstUser> list = getCurrentSession().getNamedQuery("listAnggotaKelompokByKelompokId")
					.setString("kelompokId", kelompokId).list();
		
		return list;
	}

	@Override
	public List<MstUser> getSiswaYgBelumPunyaKelompok(String userId) {
		List<MstUser> list = getCurrentSession().getNamedQuery("listSiswaTanpaKelompok").setString("userId", userId).list();
		
		return list;
	}

	@Override
	public SoalLks getSoalLksByKelompokId(String kelompokId) {
		List<SoalLks> list = getCurrentSession().getNamedQuery("listSoalLKSByKelompok").setString("kelompokId", kelompokId).list();
		if(list!=null&&list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<Kelompok> getAllKelompok() {
		
		return getCurrentSession().getNamedQuery("listAllKelompok").list();
	}

	@Override
	public List<PR> getAllPR() {
		
		return getCurrentSession().getNamedQuery("prquerygetAll").list();
	}

	@Override
	public List<Kelompok> getKelompokJoinHasilLks() {
		return getCurrentSession().getNamedQuery("getKelompokJoinHasilLks").list();
	}

	@Override
	public List<FileUpload> getAllFileUpload() {
		List<FileUpload> list = getCurrentSession().getNamedQuery("FileUpload#getAll").list();
		if(list!=null && list.size()>0){
			int no = 1;
			for(FileUpload fu:list){
				fu.setNo(no);
				no++;
			}
			return list;
		}
		
		return null;
	}

}
